package com.itheima.blog.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.api.client.BlogClient;
import com.itheima.blog.admin.dao.TBlogMapper;
import com.itheima.blog.admin.domain.po.TBlog;
import com.itheima.blog.admin.domain.po.TCategory;
import com.itheima.blog.admin.entity.Tag;
import com.itheima.blog.admin.service.ITBlogService;
import com.itheima.blog.admin.service.ITCategoryService;
import com.itheima.blog.admin.service.TagService;
import com.itheima.common.domain.BlogPageQuery;
import com.itheima.common.domain.PageResDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 博客文章表 服务实现类
 * </p>
 *
 * @author chx
 * @since 2023-07-11
 */
@Service
@RequiredArgsConstructor
public class TBlogServiceImpl extends ServiceImpl<TBlogMapper, TBlog> implements ITBlogService {
    private final BlogClient blogClient;
    private final TagService tagService;

    private final ITCategoryService categoryService;

    @Override
    public PageResDto pageQueryBlog(BlogPageQuery pageQuery) {
       // String title = pageQuery.getTitle();
        String categoryName = pageQuery.getCategoryName();
        Integer categoryId =null;
        if (categoryName!=null){
            TCategory category = categoryService.lambdaQuery().eq(categoryName!=null,TCategory::getName, categoryName).one();
            categoryId = category.getId();
            pageQuery.setCategoryId(categoryId);
        }
        String tagName = pageQuery.getTagName();
        Integer tagId =null;
        if (tagName!=null){
            Tag tag = tagService.lambdaQuery().eq(tagName!=null,Tag::getContent, tagName).one();
             tagId = tag.getId();
             pageQuery.setTagId(tagId);
        }
        PageResDto queryBlogList = blogClient.queryBlogList(pageQuery);
        return queryBlogList;
    }

    @Override
    public void deleteBlog(Integer blogId) {
        blogClient.deleteBlog(blogId);
        //TODO 博客删除后,评论和回复也要删除   评论回复删除!!!!
    }
}
